package GUI.Ventas;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Ventas.Facturacion.FacturasDelivery1;

public class CerrarVentaDelivery extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table existentes;
	private Label lblPedidosAbiertosDe;
	private Table salientes;
	private Button btnAgregarALa;
	private TableColumn tblclmnDireccionYTelefono;
	private TableColumn tblclmnArticulos;
	private TableColumn tblclmnPrecio;
	private TableColumn tblclmnKey;
	private TableColumn tableColumn;
	private TableColumn tableColumn_1;
	private TableColumn tableColumn_2;
	private TableColumn tableColumn_3;
	private TableItem tableItem;
	private Button btnCerrarVenta;
	private Button btnEliminarDeLa;
	private Label lblPedidosACerrar;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public CerrarVentaDelivery(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException, NoSalesException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException, NoSalesException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(617, 572);
		shell.setText(getText());
		
		existentes = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		existentes.setBounds(22, 57, 568, 156);
		existentes.setHeaderVisible(true);
		existentes.setLinesVisible(true);
		
		tblclmnDireccionYTelefono = new TableColumn(existentes, SWT.NONE);
		tblclmnDireccionYTelefono.setWidth(180);
		tblclmnDireccionYTelefono.setText("Direccion y Telefono");
		
		tblclmnArticulos = new TableColumn(existentes, SWT.NONE);
		tblclmnArticulos.setWidth(242);
		tblclmnArticulos.setText("Articulos");
		
		tblclmnPrecio = new TableColumn(existentes, SWT.NONE);
		tblclmnPrecio.setWidth(56);
		tblclmnPrecio.setText("Precio");
		
		tblclmnKey = new TableColumn(existentes, SWT.NONE);
		tblclmnKey.setWidth(100);
		tblclmnKey.setText("Key");
		
		
		lblPedidosAbiertosDe = new Label(shell, SWT.NONE);
		lblPedidosAbiertosDe.setBounds(22, 21, 238, 14);
		lblPedidosAbiertosDe.setText("Pedidos Abiertos de Delivery");
		
		salientes = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		salientes.setBounds(22, 293, 568, 163);
		salientes.setHeaderVisible(true);
		salientes.setLinesVisible(true);
		
		tableColumn = new TableColumn(salientes, SWT.NONE);
		tableColumn.setWidth(186);
		tableColumn.setText("Direccion y Telefono");
		
		tableColumn_1 = new TableColumn(salientes, SWT.NONE);
		tableColumn_1.setWidth(235);
		tableColumn_1.setText("Articulos");
		
		tableColumn_2 = new TableColumn(salientes, SWT.NONE);
		tableColumn_2.setWidth(71);
		tableColumn_2.setText("Precio");
		
		tableColumn_3 = new TableColumn(salientes, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("Key");
		
		btnAgregarALa = new Button(shell, SWT.NONE);
		btnAgregarALa.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			TableItem[] item = existentes.getSelection();
			TableItem t = new TableItem(salientes, SWT.NONE);
			t.setText(0, item[0].getText(0));
			t.setText(1, item[0].getText(1));
			t.setText(2, item[0].getText(2));
			t.setText(3, item[0].getText(3));
			t.setText(4, item[0].getText(4));
			existentes.remove(existentes.getSelectionIndex());
			}
		});
		btnAgregarALa.setBounds(355, 221, 157, 28);
		btnAgregarALa.setText("Agregar a la partida");
		
		btnCerrarVenta = new Button(shell, SWT.NONE);
		btnCerrarVenta.addSelectionListener(new SelectionAdapter() {
		
			public void widgetSelected(SelectionEvent e) {
				SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
				TableItem [] items = new TableItem[salientes.getItemCount()];
				items=salientes.getItems();
				SaleVO[] sales=null;
				ArrayList<SaleVO> salen = new ArrayList<SaleVO>();
				try {
					sales = uso.getOpenDeliverySales();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (NoSalesException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "No hay Ventas Abiertas de Delivery");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
		
				for(int i=0; i<sales.length;i++){
					for(int j=0;j<items.length;j++){
						if(sales[i].getSaleKey()==Integer.parseInt(items[j].getText(3))){
							salen.add(sales[i]);
							
						}
					}
				}
				shell.setVisible(false);
				FacturasDelivery1 fact = new FacturasDelivery1(shell, shell.getStyle(), salen);
				fact.open();
			
			}
		});
		btnCerrarVenta.setBounds(496, 500, 111, 28);
		btnCerrarVenta.setText("Cerrar Venta");
		
		btnEliminarDeLa = new Button(shell, SWT.NONE);
		btnEliminarDeLa.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
	
		btnEliminarDeLa.setBounds(355, 466, 157, 28);
		btnEliminarDeLa.setText("Eliminar de la partida");
		
		lblPedidosACerrar = new Label(shell, SWT.NONE);
		lblPedidosACerrar.setBounds(22, 273, 146, 14);
		lblPedidosACerrar.setText("Pedidos a Cerrar");
	
		/*tableItem = new TableItem(existentes, SWT.NONE);
		tableItem.setText(0, "Direccion Cliente" );
		tableItem.setText(1,"Hamburguesa, pizza, pizza");
		tableItem.setText(2, "32");
		tableItem.setText(3, "2");
	
		TableItem tableItem2 = new TableItem(existentes, SWT.NONE);
		tableItem2.setText(0, "Direccion Cliente" );
		tableItem2.setText(1,"Hamburguesa con queso");
		tableItem2.setText(2, "45");
		tableItem2.setText(3, "3");*/
		
		SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
		SaleVO [] sales = uso.getOpenDeliverySales();
		for(int i=0; i<sales.length;i++){
			
			String arts = "";
			ArrayList <OrderVO> ord = sales[i].getOrders();
			for(int j=0;j<ord.size();j++){
				for(int k= 0; k<ord.get(j).getArts().size(); k++){
				arts = arts + " " + ord.get(j).getArts().get(k).getName();
			}}
			tableItem = new TableItem(existentes, SWT.NONE);
			tableItem.setText(0, sales[i].getAddress() + sales[i].getTel());
			tableItem.setText(1,arts);
			tableItem.setText(2, String.valueOf(sales[i].getTotal()));
			tableItem.setText(3, String.valueOf(sales[i].getSaleKey()));
			
		
		}
		

	}
}
